package com.bnz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @Reference
    private StudentService studentService;
    
    //1. 列表学生
    @GetMapping("list")
    public R findAll(){
        List<Student> studentList = studentService.findAll();
        return R.ok().data("list",studentList);
    }

    //2. 分页查询
    @GetMapping("findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable int page,
                        @PathVariable int pageSize){
        //2.1 开始分页查询
        PageResult<Student> pr = studentService.findByPage(page,pageSize);
        //2.2 返回
        return R.ok().data("pr",pr);

    }

    //3. 条件查询带分页  @RequestBody：将前端传过来的json对象转换为java对象
    @PostMapping("search/{page}/{pageSize}")
    public R search(@PathVariable int page,
                    @PathVariable int pageSize,
                    @RequestBody StudentVo vo){
        //3.1 开始分页查询
        PageResult<Student> pr = studentService.search(page,pageSize,vo);
        //3.2 返回
        return R.ok().data("pr",pr);
    }

    //4. 添加学生
    @PostMapping("add")
    public R add(@RequestBody Student student){
        studentService.add(student);
        return R.ok().message("添加学生成功");
    }

    //5. 修改学生
    @PostMapping("update")
    public R update(@RequestBody Student student){
        studentService.update(student);
        return R.ok().message("修改学生成功");
    }

    //6. 删除学生
    @DeleteMapping("delete/{ids}")
    public R delete(@PathVariable String[] ids){
        studentService.delete(ids);
        return R.ok().message("删除学生成功");
    }
}
